package org.jeecg.modules.kuner.userpointstatement.service.impl;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.kuner.userpointstatement.entity.SdUserPointStatement;
import org.jeecg.modules.kuner.userpointstatement.mapper.SdUserPointStatementMapper;
import org.jeecg.modules.kuner.userpointstatement.service.ISdUserPointStatementService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;

/**
 * @Description: 会员积分记录
 * @Author: jeecg-boot
 * @Date: 2024-10-28
 * @Version: V1.0
 */
@Service
@Slf4j
public class SdUserPointStatementServiceImpl extends ServiceImpl<SdUserPointStatementMapper, SdUserPointStatement> implements ISdUserPointStatementService {

    /**
     * 获取今天收益积分记录条件用户ID
     *
     * @param userId 用户ID
     * @return 今天的收益记录汇总金额
     */
    @Override
    public BigDecimal getTodayPointCountByUserId(String userId) {
        return this.getBaseMapper().selectTodayPointCountByUserId(userId);
    }

    /**
     * 查询当前用户的余额积分
     *
     * @param userId 会员ID
     * @return 查询到的金额汇总数值
     */
    @Override
    public BigDecimal getTakeablePointCountByUserId(String userId) {
        return this.getBaseMapper().selectTakeablePointCountByUserId(userId);
    }

    /**
     * 用户积分变动
     *
     * @param modifyPoint  变动点数
     * @param modifyMsg    变动消息
     * @param modifyUserId 变动用户ID
     */
    @Override
    public void userModifyPoint(BigDecimal modifyPoint, String modifyMsg, String modifyUserId) {
        SdUserPointStatement userPointStatement = new SdUserPointStatement();
        userPointStatement.setModifyPoint(modifyPoint);
        userPointStatement.setUserId(modifyUserId);
        userPointStatement.setModifyMsg(modifyMsg);
        userPointStatement.setNowPoint(getTakeablePointCountByUserId(modifyUserId).add(modifyPoint));
        log.info("积分变动，用户：{}，变动积分：{}，剩余积分：{}", modifyUserId, modifyPoint, userPointStatement.getNowPoint());
        save(userPointStatement);
    }
}
